package com.example.work3.Controller;

import com.example.work3.Entity.Bus;
import com.example.work3.Service.BusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class BusController {

    @Autowired
    private BusService busService;

    @GetMapping("/getAllBus")
    public String getAllBus(){
        return busService.getAllBus().toString();
    }

    @GetMapping("/getBusById/{id}")
    public Bus getBusById(@PathVariable Integer id){
        return busService.getBusById(id);
    }

    @GetMapping("/insertBus")
    public String insertBus(){
        Bus bus = new Bus();
        bus.setRouteId(1);
        bus.setCompany("XZGJ");
        bus.setDriver("刘师傅");
        bus.setPlateNumber("xz001");
        if (busService.insertBus(bus)){
            return "添加成功";
        }else {
            return "添加失败";
        }

    }

    @GetMapping("/deleteBusById/{id}")
    public boolean deleteBusById(@PathVariable Integer id) {
        return busService.deleteBusById(id);
    }

    @GetMapping("/updateBusById/{id}")
    public boolean updateBusById(@PathVariable Integer id) {
        Bus bus = busService.getBusById(id);

        if (bus != null) {
            bus.setRouteId(00000);
            bus.setDriver("某某师傅");
            bus.setCompany("xuzhougongjiao");
            bus.setPlateNumber("xz000000");

            return busService.updateBus(bus);
        }else {
            return false;
        }
    }

}
